package com.taxsearch.controller.checkin;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.taxsearch.dao.room.RoomManagementMapper;
import com.taxsearch.service.linsa.BookRoomService;
import com.taxsearch.util.DbUtil;
import com.taxsearch.util.Json;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * 
 * @Title:BookRoomController
 * @Description: 预订房间控制器
 * @author 高敏
 * @version V1.0
 * @date 2018年8月20日
 */
@Controller
@RequestMapping("/HotelAndroid")
public class BookRoomController {
	private static Logger logger = Logger.getLogger(BookRoomController.class);
	@Resource
	private BookRoomService bookRoomService;
	
	@Autowired
	private RoomManagementMapper roommanagementmapper;

	/**
	 * 插入预订房间信息
	 * @return
	 */
	@RequestMapping("/insertBookRoom.do")
	@ResponseBody
	public Json insertExamine(HttpServletRequest request) {
		Json json = new Json();
		try {
			// 获取前端的值
			String name = request.getParameter("name");
			String tel = request.getParameter("tel");
			String roomNumber = request.getParameter("roomNumber");
			String hotelCode = request.getParameter("hotelCode");
			String checkInTime = request.getParameter("checkInTime");
			String checkOutTime = request.getParameter("checkOutTime");
			String bz = request.getParameter("bz");
			
			DbUtil.setDb("1");
			// 插入预订房间信息
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("NAME", name);
			map.put("TEL", tel);
			map.put("ROOMNUMBER", roomNumber);
			map.put("STATIONID", hotelCode);
			map.put("CHECKINTIME", checkInTime);
			map.put("CHECKOUTTIME", checkOutTime);
			map.put("BZ", bz);
			// 获取当天入住人数
			List<Map<String, String>> dangTianRuzhuList = 
					roommanagementmapper.selectRuZhuXXJQ(hotelCode, roomNumber);
			//获取当天预订人数
			List<HashMap<String,String>> ydList = 
					bookRoomService.selectBookDetail(map);
			if(dangTianRuzhuList.size()>0||ydList.size()>0){
				json.setMsg("该房间已有人员，请换房预订！");
				logger.info("该房间已有人员，请换房预订！");
				json.setSuccess(false);
			}
			
			int res = bookRoomService.insertBookRoom(map);
			logger.info("成功插入预订房间信息" + res);
			json.setObj(res);
			if(res==1){
				json.setMsg("预订成功！");
				logger.info("预订成功！");
				json.setSuccess(true);
			}else{
				json.setMsg("预订失败！");
				logger.info("预订失败！");
				json.setSuccess(false);
			}
			
		} catch (Exception e) {
			json.setMsg("服务器繁忙,请稍后再试！");
			json.setSuccess(false);
			logger.error("insertBookRoom.do-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			e.printStackTrace();
		}
		return json;
	}
	
	/**
	 * 取消预定
	 * @return
	 */
	@RequestMapping("/deleteBookRoom.do")
	@ResponseBody
	public Json deleteBookRoom(HttpServletRequest request) {
		Json json = new Json();
		try {
			// 获取前端的值
			String roomNumber = request.getParameter("roomNumber");
			String hotelCode = request.getParameter("hotelCode");
			String bookTime = request.getParameter("bookTime");
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhh24mmss");// 设置日期格式
			String date = (df.format(new Date())).substring(0, 8);// new Date()为获取当前系统时间
			if(bookTime!=null&&bookTime!=""){
				bookTime.replace("-", "").substring(0, 8);
			}else{
				bookTime=date;
			}
			
			DbUtil.setDb("1");
			// 取消预定房间信息
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ROOMNUMBER", roomNumber);
			map.put("STATIONID", hotelCode);
			map.put("CHECKINTIME", bookTime);
			int res = bookRoomService.deleteBookRoom(map);
			logger.info("取消预定" + res);
			json.setObj(res);
			if(res==1){
				json.setMsg("取消预订成功！");
				logger.info("取消预订成功！");
				json.setSuccess(true);
			}else{
				json.setMsg("取消预订失败！");
				logger.info("取消预订失败！");
				json.setSuccess(false);
			}
			
		} catch (Exception e) {
			json.setMsg("服务器繁忙,请稍后再试！");
			json.setSuccess(false);
			logger.error("deleteBookRoom.do-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			e.printStackTrace();
		}
		return json;
	}
	
	/**
	 * 查询预订列表
	 * @return
	 */
	@RequestMapping("/selectBookList.do")
	@ResponseBody
	public Json selectBookList(HttpServletRequest request) {
		Json json = new Json();
		try {
			// 获取前端的值
			String tel = request.getParameter("tel");
			String RoomType = request.getParameter("RoomType");
			String hotelCode = request.getParameter("hotelCode");
			
			DbUtil.setDb("1");
			// 查询预订列表
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("TEL", tel);
			map.put("ZT", RoomType);
			map.put("STATIONID", hotelCode);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			String date = df.format(new Date());// new Date()为获取当前系统时间
			Calendar c = Calendar.getInstance();
			c.setTime(df.parse(date));
			c.add(Calendar.DAY_OF_YEAR, -15);
			String time = df.format(c.getTime());
			map.put("CHECKOUTTIME", time);
			List<HashMap<String,String>> list = bookRoomService.selectBookList(map);
			logger.info("成功查询预订列表！");
			json.setObj(list);
			json.setMsg("查询成功！");
			logger.info("查询成功！");
			json.setSuccess(true);
		} catch (Exception e) {
			json.setMsg("服务器繁忙,请稍后再试！");
			json.setSuccess(false);
			logger.error("selectBookList.do-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			e.printStackTrace();
		}
		return json;
	}
	
	/**
	 * 查询预订详情
	 * @return
	 */
	@RequestMapping("/selectBookDetail.do")
	@ResponseBody
	public Json selectBookDetail(HttpServletRequest request) {
		Json json = new Json();
		try {
			// 获取前端的值
			String roomNumber = request.getParameter("roomNumber");
			String hotelCode = request.getParameter("hotelCode");
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhh24mmss");// 设置日期格式
			String date = (df.format(new Date())).substring(0, 8);// new Date()为获取当前系统时间
			
			DbUtil.setDb("1");
			// 查询预订详情
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ROOMNUMBER", roomNumber);
			map.put("STATIONID", hotelCode);
			map.put("CHECKINTIME", date);
			List<HashMap<String,String>> list = bookRoomService.selectBookDetail(map);
			logger.info("成功查询预订详情！");
			json.setObj(list);
			json.setMsg("查询成功！");
			logger.info("查询成功！");
			json.setSuccess(true);
		} catch (Exception e) {
			json.setMsg("服务器繁忙,请稍后再试！");
			json.setSuccess(false);
			logger.error("selectBookDetail.do-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			e.printStackTrace();
		}
		return json;
	}

}
